package com.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.health.constant.MessageConstant;
import com.health.dao.CheckGroupDao;
import com.health.dao.SetmealDao;
import com.health.entity.PageResult;
import com.health.entity.QueryPageBean;
import com.health.entity.Result;
import com.health.pojo.CheckGroup;
import com.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @Classname CheckItemGroupServiceImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/16 10:10
 * @Author RainGrd
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private SetmealDao setmealDao;


    @Override
    public PageResult queryCheckGroupPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckGroup> checkGroupList = checkGroupDao.selectByCondition(queryPageBean.getQueryString());
        log.info("查询出来的数据列表:{}", checkGroupList);
        long count = checkGroupDao.selectTotalByCondition(queryPageBean.getQueryString());
        log.info("查询出来的总数:{}", count);
        return new PageResult(count, checkGroupList);
    }

    @Override
    public Result saveCheckGroup(CheckGroup checkGroup, Integer[] checkItemIds) {
        try {
            int i = checkGroupDao.insertCheckGroup(checkGroup);
            setCheckGroupAndCheckItem(checkGroup.getId(), checkItemIds);
            return i > 0 ? Result.success(MessageConstant.ADD_CHECKGROUP_SUCCESS) : Result.error(MessageConstant.ADD_CHECKGROUP_FAIL);
        } catch (Exception e) {

            return Result.error(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @Override
    public Result queryCheckItemIdsByCheckGroupId(Integer id) {
        try {
            Integer[] checkItemIds = checkGroupDao.selectCheckItemIdsByCheckGroupId(id);
            log.info("checkItemIds:{}", Arrays.asList(checkItemIds));
            return Result.success(checkItemIds);
            /*if (checkItemIds.length >= 1) {
                return Result.success(checkItemIds, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            }
            return Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);*/
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.success();
        }

    }

    @Override
    public Result editCheckGroup(CheckGroup checkGroup, Integer[] checkItemIds) {
        try {
            //判断根据检查组id查询套餐id
            //根据检查组id删除中间表数据（清理原有关联关系）
            checkGroupDao.deleteAssociation(checkGroup.getId());
            //重新补充
            setCheckGroupAndCheckItem(checkGroup.getId(), checkItemIds);
            //正式编辑检查组
            int i = checkGroupDao.updateCheckGroup(checkGroup);
            return i > 0 ? Result.success(MessageConstant.EDIT_CHECKGROUP_SUCCESS) : Result.error(MessageConstant.EDIT_CHECKGROUP_FAIL);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }

    @Override
    public Result clearCheckGroup(Integer id) {
        //删除检查组
        try {
            //根据检查组id删除中间表数据（清理原有关联关系）
            checkGroupDao.deleteAssociation(id);
            //根据检查组Id查询套餐Id
            List<Integer> ids = setmealDao.selectSetMealIdsByCheckGroupId(id);
            if (ids.size()>0) {
                return Result.error("此检查组已经被套餐选中!");
            }
            //删除检查组
            int i = checkGroupDao.deleteCheckGroup(id);
            return i > 0 ? Result.success(MessageConstant.DELETE_CHECKGROUP_SUCCESS) : Result.error(MessageConstant.DELETE_CHECKGROUP_FAIL);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }

    @Override
    public Result queryAll() {
        List<CheckGroup> checkGroupList = checkGroupDao.selectAll();
        if (checkGroupList != null && checkGroupList.size() > 0) {
            return Result.success(checkGroupList);
        }
        return Result.error(MessageConstant.QUERY_CHECKGROUP_SUCCESS);
    }

    /**
     * 用于设置套餐与检查组的关系
     *
     * @param checkGroupId
     * @param checkItemIds
     */
    private void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkItemIds) {
        //判断检查项目项Id数组是否为空
        if (checkItemIds != null && checkItemIds.length > 0) {
            //添加检查组和检查项
            for (Integer checkItemId : checkItemIds) {
                checkGroupDao.insertCheckGroupAndCheckItem(checkGroupId, checkItemId);
            }
        }
    }
}
